package com.shinektv.lrclib;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.shinektv.vod.ksclib.KscLine;

import java.util.ArrayList;
import java.util.List;

public class LrcControlManager {
    private Context context;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mLayoutParams;
    private View mView;
    private LrcTextView lrcTextView1, lrcTextView2;

    public LrcControlManager(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT, 0, 0, PixelFormat.TRANSPARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//9.0
            mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        mView = LayoutInflater.from(context).inflate(R.layout.lrc_layout, null);
        lrcTextView1 = mView.findViewById(R.id.lrcTextView1);
        lrcTextView2 = mView.findViewById(R.id.lrcTextView2);
        lrcTextView2.alignWithParent();
    }

    //显示歌词
    public void showLrc() {
        if (mView.getTag() == null) {
            mWindowManager.addView(mView, mLayoutParams);
            mView.setTag("VISIBLE");
        } else {
            mWindowManager.removeView(mView);
            mWindowManager.addView(mView, mLayoutParams);
        }
    }

    public void hideLrc() {
        if (mWindowManager == null) return;
        if (mView != null) {
            if (mView.getTag() != null) {
                mWindowManager.removeView(mView);
            }
            mView.setTag(null);
        }
    }


    //演唱到第几行歌词
    private int index = 0;
    private List<KscLine> kscLines = new ArrayList<>();
    //提前多长时间显示歌词
    final int TIME_INTERVAL = 2000;
    //唱完以后多久隐藏歌词
    final int TIME_INTERVAL_END = 5000;

    //将歌词内容传递进来
    public void setKscLines(List<KscLine> kscLines) {
        if (null == kscLines) {
            return;
        }
        this.kscLines.clear();
        this.kscLines.addAll(kscLines);
        showLrc();
    }

    //根据ksc显示歌词
    public void showWordsOfSong(long curTime) {
        if (null == kscLines) {
            return;
        }
        if (index == 0) {
            if (kscLines.size() > 0
                    && curTime >= kscLines.get(0).getLine_start_time() - TIME_INTERVAL
                    && curTime < kscLines.get(0).getLine_start_time()) {
                if (!lrcTextView1.getLrc().equals(kscLines.get(0).getLine_text())) {
                    lrcTextView1.setLrc(kscLines.get(0).getLine_text());
                    lrcTextView1.setTag(kscLines.get(0));
                }
                if (!lrcTextView2.getLrc().equals(kscLines.get(1).getLine_text())) {
                    lrcTextView2.setLrc(kscLines.get(1).getLine_text());
                    lrcTextView2.setTag(kscLines.get(1));
                }
            }
        }
        if (index < kscLines.size()) {
            KscLine kscLine = kscLines.get(index);
            //提前3000毫秒显示歌词
            if (curTime >= (kscLine.getLine_start_time() - TIME_INTERVAL) && curTime <= kscLine.getLine_start_time()) {
                if (index % 2 == 0) {
                    if (!lrcTextView1.getLrc().equals(kscLine.getLine_text())) {
                        lrcTextView1.setTag(kscLine);
                        lrcTextView1.setLrc(kscLine.getLine_text());
                        lrcTextView1.removeCallbacks(runnable);
                        lrcTextView1.postDelayed(runnable, kscLine.getLine_stop_time() - kscLine.getLine_start_time() + TIME_INTERVAL_END + (kscLine.getLine_start_time() - curTime));
                    }
                } else {
                    if (!lrcTextView2.getLrc().equals(kscLine.getLine_text())) {
                        lrcTextView2.setTag(kscLine);
                        lrcTextView2.setLrc(kscLine.getLine_text());
                        lrcTextView2.removeCallbacks(runnable2);
                        lrcTextView2.postDelayed(runnable2, kscLine.getLine_stop_time() - kscLine.getLine_start_time() + TIME_INTERVAL_END + (kscLine.getLine_start_time() - curTime));
                    }
                }
                index++;
            }
        }

        //某句歌词
        if (null != lrcTextView1.getTag()
                && curTime >= ((KscLine) (lrcTextView1.getTag())).getLine_start_time()
                && curTime <= ((KscLine) (lrcTextView1.getTag())).getLine_stop_time()) {
            long lrcTimeLong = ((KscLine) (lrcTextView1.getTag())).getLine_stop_time() - ((KscLine) (lrcTextView1.getTag())).getLine_start_time();
            long selectLong = curTime - ((KscLine) (lrcTextView1.getTag())).getLine_start_time();
            float percentage = (float) ((double) selectLong / (double) lrcTimeLong);
            lrcTextView1.setPercentage(percentage);
        } else if (null != lrcTextView2.getTag()
                && curTime >= ((KscLine) (lrcTextView2.getTag())).getLine_start_time()
                && curTime <= ((KscLine) (lrcTextView2.getTag())).getLine_stop_time()) {
            long lrcTimeLong = ((KscLine) (lrcTextView2.getTag())).getLine_stop_time() - ((KscLine) (lrcTextView2.getTag())).getLine_start_time();
            long selectLong = curTime - ((KscLine) (lrcTextView2.getTag())).getLine_start_time();
            float percentage = (float) ((double) selectLong / (double) lrcTimeLong);
            lrcTextView2.setPercentage(percentage);
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (lrcTextView1.getLrc().equals(((KscLine) (lrcTextView1.getTag())).getLine_text())) {
                lrcTextView1.setLrc("");
            }
        }
    };
    Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            if (lrcTextView2.getLrc().equals(((KscLine) (lrcTextView2.getTag())).getLine_text())) {
                lrcTextView2.setLrc("");
            }
        }
    };
}
